const path = require('path')
const autoprefixer = require('autoprefixer')
const pxtoviewport = require('postcss-px-to-viewport')

const debug = process.env.NODE_ENV === 'production' ? false : true
function resolve (dir) {
  return path.join(__dirname, dir)
}
module.exports = {
  publicPath: './',
  productionSourceMap: debug,
  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          autoprefixer(),
          pxtoviewport({
            viewportWidth: 375
          })
        ]
      },
      less: {
        modifyVars: {
          // 或者可以通过 less 文件覆盖（文件路径为绝对路径）
          hack: `true; @import "${path.join(__dirname, 'src/theme.less')}";`,
        },
      },
    }
  },
  devServer: {
    port: 3001,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      '/api': {
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: {
          '^/api': 'api'
        }
      },
      '/tsdb': {
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: {
          '^/tsdb': 'tsdb'
        }
      },
      '/iot': {
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: {
          '^/tsdb': 'tsdb'
        }
      },
      '/iot': {
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: {
          '^/tsdb': 'tsdb'
        }
      },
      '/iot': {
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: {
          '^/iot': 'iot'
        }
      },
      '/equ': {
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: {
          '^/equ': 'equ'
        }
      },
    }
  },
  // 路径匹配
  chainWebpack: config => {
    config.resolve.alias
    .set("@", resolve(`src`))
    .set("@utils", resolve(`src/utils`))
    .set("@api", resolve(`src/api`))
    .set("@store", resolve(`src/store`))
    .set("@views", resolve(`src/views`))
    // cdn引入
    config.externals = {
      "vue": "Vue",
      "axios":"axios",
      "VueRouter": "VueRouter",
      "Cookies": "js-cookie",
      "moment": "moment",
      "echarts": "echarts",
      "Vuex": "Vuex",
      "wx": "wx",
    }
  }
};